<?php

namespace app\admin\service\admin;

use app\admin\model\admin\AdminMenu;
use app\admin\service\BaseService;
use support\Response;
use support\think\Cache;
use extend\category\Category;

class MenuService extends BaseService
{
    public function __construct(AdminMenu $model)
    {
        $this->model = $model;
    }

    public function loadList(array $where, int $page, int $limit, $field = '*', string $order = 'sort asc,id asc'): array
    {
        $model = $this->model->where($where)->field($field);
        $list = $model->page($page, $limit)->order($order)->select()->toArray();
        $count = $model->count();

        return compact('list', 'count');
    }

    public function getDataList(array $where, $field = '*', string $order = 'sort asc,id asc'): array
    {
        $model = $this->model->where($where)->field($field);
        $list = $model->order($order)->select()->toArray();
        $count = $model->count();

        return compact('list', 'count');
    }

    public function getAdminMenuInfo($type, $url_menu_id = '', $act_menu_id = ''): array
    {
        if (Cache::has('menu_list')) {
            $menu_list = Cache::get('menu_list');
        } else {
            $where = [];
            $where[] = ['status', '=', 1];
            $menu_list = $this->model->where($where)->order('sort', 'ASC')->order('id', 'ASC')->select()->toArray();
            Cache::set('menu_list', $menu_list);
        }

        $urlArr = [];
        if ($url_menu_id != '') {
            $urlArr = array_filter(explode(',', $url_menu_id), function ($value) {
                return !empty($value);
            });
        }
        $actArr = [];
        if ($act_menu_id != '') {
            $actArr = array_filter(explode(',', $act_menu_id), function ($value) {
                return !empty($value);
            });
        }

        $urlAry = [];
        $actAry = [];
        foreach ($menu_list as $k => $v) {
            if ($type == 1) {
                if ($v['is_menu'] == 1) {
                    $urlAry[] = $v;
                } else {
                    $actAry[] = $v['act'];
                }
            } else {
                if ($v['is_menu'] == 1) {
                    if (in_array($v['id'], $urlArr)) {
                        $urlAry[] = $v;
                    }
                } else {
                    if (in_array($v['id'], $actArr)) {
                        $actAry[] = $v['act'];
                    }
                }
            }
        }

        if (!empty($urlAry)) {
            foreach ($urlAry as $k => $v) {
                $urlAry[$k]['url'] = $v['url'] ? '#/' . $v['url'] : '';
            }
        }
        $roles = '';
        if (!empty($actAry)) {
            $roles = ',' . implode(',', $actAry) . ',';
        }

        return ['menus' => $urlAry, 'roles' => $roles];
    }

    public function setMenu($data)
    {
        if (!isset($data['id']) || empty($data['id'])) {
            $getId = $this->model->insertGetId($data);
        } else {
            $getId = $data['id'];
            $menuInfo = $this->model->find($getId);
            unset($data['id']);
            $menuInfo->save($data);
        }

        if ($getId) {
            return json(['code' => 1, 'msg' => '保存成功']);
        } else {
            return json(['code' => -1, 'msg' => '保存失败']);
        }
    }

    public function setMenuAll($data)
    {
        $this->model->saveAll($data);
        return json(['code' => 1, 'msg' => '保存成功']);
    }

    public function delMenu($data)
    {
        $this->model::destroy($data['id']);
        return json(['code' => 1, 'msg' => '删除成功']);
    }

}